package org.whuims.pois.action;

import org.apache.logging.log4j.core.helpers.Strings;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.action.result.SiteVo;
import org.whuims.pois.action.result.SpiderVo;
import org.whuims.pois.dao.SiteDao;
import org.whuims.pois.dao.SpiderDao;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.SiteC;
import org.whuims.pois.model.SpiderC;
import org.whuims.pois.model.TopicC;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhongwei on 2014/5/15.
 */
public class SpiderAction extends DispatchAction {
    BaseDao baseDao = new BaseDao();
    SpiderDao spiderDao = new SpiderDao();
    SiteDao siteDao = new SiteDao();

    public ActionForward show(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {
        long topicId = getTopic(request);
        if (topicId <= 0) {
            return mapping.findForward("login");
        }
        List<SpiderVo> list = spiderDao.selectList(topicId);
        request.setAttribute("spiderVo", list);
        return mapping.findForward("show");
    }

    public ActionForward add(ActionMapping mapping, ActionForm form,
                             HttpServletRequest request, HttpServletResponse response) {
        List<SiteC> list = siteDao.select();
        int num = list.size();
        long topicId = getTopic(request);
        if (topicId <= 0) {
            return mapping.findForward("login");
        }
        TopicC topicC = (TopicC) baseDao.getByPk(TopicC.class, topicId);
        if (topicC == null) {
            return mapping.findForward("login");
        }
        request.setAttribute("topicName", topicC.getName());
        String[] siteNameArray = new String[num];
        List<SiteVo> siteVos = new ArrayList<SiteVo>();
        for (int i = 0; i < num; i++) {
            siteVos.add(new SiteVo(list.get(i).getId(), list.get(i).getSiteName()));
            siteNameArray[i] = list.get(i).getSiteName();
        }

        request.setAttribute("siteVo", siteVos);
        return mapping.findForward("add");
    }

    private long getTopic(HttpServletRequest request) {
        Object topic = request.getSession().getAttribute("topicId");
        if (topic == null) {
            return -1;
        }
        return (Long) topic;
    }

    public ActionForward insert(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        SpiderC bean = (SpiderC) form;
        if (bean.getTopicId() <= 0) {
            bean.setTopicId((Long) request.getSession().getAttribute("topicId"));
        }
        SiteC siteC = (SiteC) baseDao.getByPk(SiteC.class, bean.getSite());
        if (siteC != null) {
            bean.setSitename(siteC.getSiteName());
        }
        bean.setdType(siteC.getDtype());
        String topicName = "";
        TopicC topicC = (TopicC) baseDao.getByPk(TopicC.class, bean.getTopicId());
        if (topicC != null) {
            topicName = topicC.getName();
        }

        if (Strings.isEmpty(bean.getName())) {
            bean.setName(topicName + ":" + bean.getSitename() + ":" + bean.getCrawlertype());
        }
        spiderDao.insert(bean);
        return mapping.findForward("success");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {
        String spiderId = request.getParameter("id");
        Long id = Long.parseLong(spiderId);
        SpiderC bean = spiderDao.select(id);
        request.setAttribute("bean", bean);
        return mapping.findForward("edit");
    }

//    public ActionForward modify(ActionMapping mapping, ActionForm form,
//                                HttpServletRequest request, HttpServletResponse response) {
//        SpiderC bean = (SpiderC)form;
//        if(bean.getSiteName().trim().equals("")){
//            request.setAttribute("errormessage", "站点名不能为空");
//            return mapping.findForward("error");
//        }
//        spiderDao.modify(bean);
//        return mapping.findForward("success");
//    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        String spiderId = request.getParameter("spiderId");
        Long id = Long.parseLong(spiderId);
        SpiderC bean = spiderDao.select(id);
        spiderDao.delete(bean);
        return mapping.findForward("success");
    }
}
